package cn.csc.module.pay.controller.admin.bank;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.common.pojo.CommonResult;
import static cn.csc.framework.common.pojo.CommonResult.success;

import cn.csc.framework.excel.core.util.ExcelUtils;

import cn.csc.framework.operatelog.core.annotations.OperateLog;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.csc.module.pay.controller.admin.bank.vo.*;
import cn.csc.module.pay.dal.dataobject.bank.PayBankDO;
import cn.csc.module.pay.convert.bank.PayBankConvert;
import cn.csc.module.pay.service.bank.PayBankService;

@Api(tags = "管理后台 - 资金账户绑定的银行卡")
@RestController
@RequestMapping("/pay/bank")
@Validated
public class PayBankController {

    @Resource
    private PayBankService bankService;

    @PostMapping("/create")
    @ApiOperation("创建资金账户绑定的银行卡")
    @PreAuthorize("@ss.hasPermission('pay:bank:create')")
    public CommonResult<Long> createBank(@Valid @RequestBody PayBankCreateReqVO createReqVO) {
        return success(bankService.createBank(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新资金账户绑定的银行卡")
    @PreAuthorize("@ss.hasPermission('pay:bank:update')")
    public CommonResult<Boolean> updateBank(@Valid @RequestBody PayBankUpdateReqVO updateReqVO) {
        bankService.updateBank(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除资金账户绑定的银行卡")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('pay:bank:delete')")
    public CommonResult<Boolean> deleteBank(@RequestParam("id") Long id) {
        bankService.deleteBank(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得资金账户绑定的银行卡")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('pay:bank:query')")
    public CommonResult<PayBankRespVO> getBank(@RequestParam("id") Long id) {
        PayBankDO bank = bankService.getBank(id);
        return success(PayBankConvert.INSTANCE.convert(bank));
    }

    @GetMapping("/list")
    @ApiOperation("获得资金账户绑定的银行卡列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('pay:bank:query')")
    public CommonResult<List<PayBankRespVO>> getBankList(@RequestParam("ids") Collection<Long> ids) {
        List<PayBankDO> list = bankService.getBankList(ids);
        return success(PayBankConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得资金账户绑定的银行卡分页")
    @PreAuthorize("@ss.hasPermission('pay:bank:query')")
    public CommonResult<PageResult<PayBankRespVO>> getBankPage(@Valid PayBankPageReqVO pageVO) {
        PageResult<PayBankDO> pageResult = bankService.getBankPage(pageVO);
        return success(PayBankConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出资金账户绑定的银行卡 Excel")
    @PreAuthorize("@ss.hasPermission('pay:bank:export')")
    @OperateLog(type = EXPORT)
    public void exportBankExcel(@Valid PayBankExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PayBankDO> list = bankService.getBankList(exportReqVO);
        // 导出 Excel
        List<PayBankExcelVO> datas = PayBankConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "资金账户绑定的银行卡.xls", "数据", PayBankExcelVO.class, datas);
    }

}
